<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'add_recom':
      add_recom($myPdo);
      break;
    case 'del_recom':
      del_recom($myPdo);
      break;
    case 'up':
      up($myPdo);
      break;
    case 'down':
      down($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function add_recom($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = MiscUtils::getParam('q', 0);
  
  $opt = new stdClass();
  $opt->select_expr = 'MIN(t.order_number) AS min';
  $min = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), NULL, NULL, NULL, NULL, NULL, $opt)->d[0]->min;
  if (!$min) {
    $min = 0;
  }
  
  $obj = new stdClass();
  $obj->order_number = $min - 1;
  $obj->product_id = $q;
  DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $obj);
  echo json_encode(array());
}

function del_recom($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = MiscUtils::getParam('q', 0);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 'id = :v1';
  $cond_vals->v = array(':v1' => $q);
  DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals);
  
  echo json_encode(array());
}

function up($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $id = MiscUtils::getParam('id', 0);
  $num = MiscUtils::getParam('num', 0);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 'order_number < :v1';
  $cond_vals->v = array(':v1' => $num);
  $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals, 't.order_number', 'DESC', 1, 1, NULL)->d;
  
  if (count($tmp) > 0) {
    $obj = new stdClass();
    $obj->id = $id;
    $obj->order_number = $tmp[0]->order_number;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $obj);
    
    $tmp[0]->order_number = $num;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $tmp[0]);
  }
  echo json_encode(array());
}

function down($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $id = MiscUtils::getParam('id', 0);
  $num = MiscUtils::getParam('num', 0);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 'order_number > :v1';
  $cond_vals->v = array(':v1' => $num);
  $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals, 't.order_number', 'ASC', 1, 1, NULL)->d;
  
  if (count($tmp) > 0) {
    $obj = new stdClass();
    $obj->id = $id;
    $obj->order_number = $tmp[0]->order_number;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $obj);
    
    $tmp[0]->order_number = $num;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $tmp[0]);
  }
  echo json_encode(array());
}
?>